package com.likou.second_challenge;

import java.util.ArrayList;
import java.util.List;

public class q114 {

    List<TreeNode> list = new ArrayList<>();

    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        DFS(root);

        root.left = null;
        root.right = null;

        for (int i = 1; i < list.size(); i++) {
            list.get(i).left = null;
            root.right = list.get(i);
            root = root.right;
        }
    }

    public void DFS(TreeNode root) {
        if (root == null) {
            return;
        }

        list.add(root);
        DFS(root.left);
        DFS(root.right);
    }

}
